package com.kgc.springboot.hangduo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.hangduo.pojo.Client;
import com.kgc.springboot.hangduo.pojo.Laws;
import com.kgc.springboot.hangduo.pojo.User;
import com.kgc.springboot.hangduo.service.ClientService;
import com.kgc.springboot.hangduo.service.LawsService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

@Controller
public class ClientController {
    @Autowired
    private ClientService clientService;
    @Autowired
    LawsService lawsService;

    //分页查询
    @RequestMapping("/member-list.html")
//    @ResponseBody
    public String selectClientAll(@RequestParam(value = "clientPhone",required = false) String clientPhone, @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, Model model){
        Integer pageSize=5;
        System.out.println("查询........");
        PageInfo<Client> pageInfo = clientService.selectClientAll(clientPhone, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("clientPhone",clientPhone);
        System.out.println("跳转啦");
        return "member-list";
    }
    //前端登录
    @RequestMapping("/touser-login")
    public String toLogin(){
        return "user/login";
    }
    @RequestMapping("/user-login")
    public String Login(String phone, String password, HttpSession session, Model model){
        Client client = clientService.login(phone, password);
        if(client!=null){
            client.setCreateTime(new Date());
            session.setAttribute("client",client);
            System.out.println(client);
            List<Laws> laws = lawsService.selectAll();
            model.addAttribute("laws",laws);
            return "user-index";
        }else {
            model.addAttribute("msg","用户名或密码错误，请重新登录！！！");
            return "user/login";
        }
    }
    //用户注册
    @RequestMapping("/touser-register")
    public String toUserRegister(){
        return "user/register";
    }
    //用户注册
    @RequestMapping("/user-register")
    public String UserRegister(Client client,HttpSession session,Model model){
        //System.out.println(user.getUserName()+"mmmmmm");
        Client client1 = clientService.getPhone(client.getClientPhone());
        if (client1==null){
            //user.setUserName(userName);
            //user.setTelephone(telephone);
            String password1 = client.getClientPassword();
            client.setClientPassword(password1);
            int i = clientService.insertSelective(client);
            if(i>0){
                session.setAttribute("client",client);
                System.out.println(client);
            }else {
                model.addAttribute("msg","注册失败，请重新注册或联系管理员");
            }
        }else {
            model.addAttribute("msg","账号重复，请换个手机号注册");
        }
        return "redirect:/touser-login";
    }


    //验证手机号是否存在
    @RequestMapping("/user-getPhone")
    @ResponseBody
    public String getPhone(String phone, HttpServletResponse response) throws IOException {
        Client client = clientService.getPhone(phone);
        //System.out.println("user"+user.toString());
        String msg="";
        if (client != null) {

            /*System.out.println("222222");
           response.getWriter().println("0");*/
            msg="0";
        } else {
            System.out.println("111111");
            //response.getWriter().print("1");
            msg="1";
        }
        return msg;
    }

    @RequestMapping("/updatepwd")
    public String updatepwd(HttpSession session,Model model){
        Client client = (Client) session.getAttribute("client");
        if(client==null)return "redirect:/touser-login";
        return "/user/userpwd";
    }
    @RequestMapping("/user-logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "/user/login";
    }

    @RequestMapping("/updateClitpwd")
    public String updateClitpwd(String oldpwd,String newpwd,HttpSession session,Model model){
        Client client = (Client) session.getAttribute("client");
        if (client.getClientPassword().equals(oldpwd)){
            //修改密码
            System.out.println(oldpwd);
            System.out.println(newpwd);
            client.setClientPassword(newpwd);
            clientService.updateById(client);
            session.invalidate();
            return "redirect:/updatepwd";
        }else {
            //修改失败
            model.addAttribute("msg","修改失败");
            if(client==null)return "redirect:/touser-login";
            return "/user/userpwd";
        }

    }

}
